//import CostItem from "../controller/ui/cost_items"

import { Skill } from "../class/skill"
import { GameBalance } from "../data/GameBalance"
import RoomType from "../entity/RoomType"
import DD from "../manager/dynamic_data_manager"
import JsonManager from "../manager/json_manager"
import MainManager from "../manager/main_manager"
import StorageManager from "../manager/storage_manager"
import UIManager from "../manager/ui_manager"
import { Emitter } from "../utils/emmiter"
import { MessageType } from "../utils/message"
import { Utils } from "../utils/utils"
import TipsUIManager from "./tip_ui_manager"

// import UIAnimaManager from "../manager/action_manager"

const { ccclass, property } = cc._decorator

interface OkCalBack {
    (isOK: boolean, param?: {}): void
}

@ccclass
export default class LevelListUIManager extends cc.Component {
    static instance: LevelListUIManager = null

    @property(cc.Node)
    uiNode: cc.Node = null

    @property(cc.Node)
    maskNode: cc.Node = null

    @property(cc.Node)
    equipNode: cc.Node = null

    @property(cc.Label)
    labelTitle: cc.Label = null

    @property(cc.Label)
    labelDesc: cc.Label = null

    @property(cc.Label)
    debugLabel: cc.Label = null

    @property(cc.Label)
    moneyLabel: cc.Label = null

    @property(cc.Label)
    ticketLabel: cc.Label = null

    @property(cc.Button)
    nextBtn: cc.Button = null
    @property(cc.Button)
    cheatBtn: cc.Button = null

    @property(cc.Button)
    newGameBtn: cc.Button = null

    @property(cc.Button)
    prevBtn: cc.Button = null

    @property(cc.Button)
    openCharactorBtn: cc.Button = null

    @property(cc.Button)
    openSkillBtn: cc.Button = null


    @property(cc.Button)
    openStatBtn: cc.Button = null

    @property(cc.Button)
    buttonConfirm: cc.Button = null

    @property(cc.Label)
    curLevelLable: cc.Label = null


    curCallBack: OkCalBack = null
    curParam: any = null


    _isClick: boolean = false

    onLoad() {
        // console.log("PoolManager Onload");
        LevelListUIManager.instance = this

        //this.maskNode.on('click', this.hideUI, this);

        this.nextBtn.node.on("click", this.selectNextLeve, this)
        this.prevBtn.node.on("click", this.selectPreLeve, this)

        this.openCharactorBtn.node.on("click", this.openCharator, this)
        this.openSkillBtn.node.on("click", this.openSkill, this)
        this.openStatBtn.node.on("click", this.openStat, this)
        this.buttonConfirm.node.on("click", this.enterGame, this)
        this.cheatBtn.node.on("click", this.upLevel, this)
        this.newGameBtn.node.on("click", this.newGame, this)

        if (this.uiNode.active) {
            this.uiNode.active = false
        }

        Emitter.register(MessageType.levelIncrease, (name, data) => {
            this.selectNextLeve()

        }, '')
    }

    openSkill() {
        Emitter.fire(MessageType.openSkillList)
    }

    openCharator() {
        Emitter.fire(MessageType.openCharactor)
    }

    openStat() {
        Emitter.fire(MessageType.openExtraStat)
    }




    showUI(title: string, desc: string, callback: OkCalBack = null, param: {} = null, isNormal: boolean = true, labels: string[] = null) {
        if (!this.uiNode.active) {
            this.uiNode.active = true
            //  UIAnimaManager.instance.showDialog(this.uiNode, this.maskNode)
        }



        this.refreshUI()
        this.curParam = param
    }

    newGame() {
        UIManager.instance.showTip('清除成功')
        StorageManager.instance.removeDataByKey('userdata')
    }

    upLevel() {
        StorageManager.instance.levelInfo.maxLevel++
        StorageManager.instance.levelInfo.money += 10000;
        StorageManager.instance.remainSkillTicket += 50
        GameBalance.debugHps(StorageManager.instance.levelInfo.curLevel)
        this.updateDebug()
    }

    updateDebug() {
        this.debugLabel.string = "子弹攻击力" + GameBalance.calculateSkillAtk("5")
    }

    refreshUI() {
        let index = StorageManager.instance.levelInfo.curLevel - 1
        // this.curLevelLable.string = '第'+StorageManager.instance.levelInfo.curLevel+'关';
        this.updateRoomUI(DD.instance.rooms[index])
        // this.curLevelLable.string = DD.instance.rooms[index].
        this.moneyLabel.string = StorageManager.instance.levelInfo.money.toString()
        this.equipNode.removeAllChildren(); // 清空之前的内容
        let specidl = JsonManager.instance.getDataByName('skill_special')
        let tmp = Object.values(specidl)
        let bonusData = []
        tmp.forEach(v => {
            if (v.skillId == this.id) {
                bonusData.push(v)
            }
        })
        cc.log(bonusData)
        // PoolManager
        bonusData.forEach((data, index) => {

            // let item = PoolManager.instance.createObjectByName('speicia_list_btn', this.content1)
        })// this.ticketLabel.string = StorageManager.instance.levelInfo.ski.toString()
    }

    updateRoomUI(roomType) {
        switch (roomType) {
            case RoomType.Normal:
                this.curLevelLable.string = "普通房间";
                break;
            case RoomType.Elite:
                this.curLevelLable.string = "精英房间";
                break;
            case RoomType.Shop:
                this.curLevelLable.string = "商店房间";
                break;
            case RoomType.Bonfire:
                this.curLevelLable.string = "篝火房间";
                break;
            case RoomType.Boss:
                this.curLevelLable.string = "Boss房间";
                break;
        }
        this.curLevelLable.string = StorageManager.instance.levelInfo.curLevel + this.curLevelLable.string
    }

    selectNextLeve() {
        StorageManager.instance.levelInfo.curLevel++;

        if (StorageManager.instance.levelInfo.curLevel >= StorageManager.instance.levelInfo.maxLevel) {
            StorageManager.instance.levelInfo.curLevel = StorageManager.instance.levelInfo.maxLevel
        }
        this.refreshUI()
    }

    selectPreLeve() {
        StorageManager.instance.levelInfo.curLevel--;
        if (StorageManager.instance.levelInfo.curLevel < 1) {
            StorageManager.instance.levelInfo.curLevel = 1;
        }
        this.refreshUI()
    }

    enterGame() {
        cc.log('1')
        // StorageManager.instance.skills.push(this.ids[0])
        // this.notifyAdd()
        Emitter.fire(MessageType.gameStart)
        this.hideUI()
    }



    confirm() {

        this.hideUI()
    }
    hideUI() {
        this.uiNode.active = false
        cc.director.resume();
        //  AudioManager.instance.playAudio('ui_close')

    }
}
